package com.neusoft.neuiotms.dcom.config;

import java.util.Map;
import java.util.concurrent.Executors;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

import com.google.common.eventbus.AsyncEventBus;
import com.neusoft.bizcore.activiti.user.ActivitiUserDetail;
import com.neusoft.bizcore.auth.common.bean.UserBean;
import com.neusoft.bizcore.auth.common.service.AuthUserDetailsService;
import com.neusoft.bizcore.web.dto.result.ResultDTO;
import com.neusoft.bizcore.web.utils.id.BaseIdGenerator;
import com.neusoft.bizcore.web.utils.id.DefaultIdGenerator;
import com.neusoft.bizcore.web.utils.id.TimeIdGenerator;
import com.neusoft.neuiotms.dcom.constants.DCOMConstants;
import com.neusoft.neuiotms.micro.auth.AuthMicroService;

import io.netty.util.concurrent.DefaultThreadFactory;

@Configuration
@ComponentScan
public class DcomConfiguration {

    @Value("${databus.core.modelUpdateEventThreadSize: 1}")
    private int modelUpdateEventThreadSize;

    @Bean
    @ConditionalOnClass(ActivitiUserDetail.class)
    @ConditionalOnMissingBean
    public ActivitiUserDetail userAdapter(final AuthMicroService authMicroService) {
        final ActivitiUserDetail adapter = new ActivitiUserDetail() {
            @Override
            public UserBean getUserInfo(final String userName) {
                final ResultDTO<UserBean> result = authMicroService.loadUserByUsername(userName);
                return result.getData();
            }

            @Override
            public Map<String, Object> getRoleInfo(final String code) {
                return authMicroService.roleByCode(code).getData();
            }
        };
        return adapter;
    }

    @Bean
    public AsyncEventBus modelUpdateEventBus() {
        return new AsyncEventBus(DCOMConstants.MAINTAINPLAN_EVENTBUS_ID,
                Executors.newFixedThreadPool(this.modelUpdateEventThreadSize,
                        new DefaultThreadFactory(DCOMConstants.MAINTAINPLAN_EVENTBUS_ID)));
    }

    @Bean
    @ConditionalOnMissingBean(AuthUserDetailsService.class)
    public AuthUserDetailsService authUserDetailsService(final AuthMicroService authMicroService) {
        return username -> {
            final ResultDTO<UserBean> result = authMicroService.loadUserByUsername(username);
            return result.getData();
        };
    }

    @Bean("maintainRuleCodeGenerator")
    public BaseIdGenerator maintainRuleCodeGenerator() {
        return new DefaultIdGenerator(0, "WHR");
    }

    @Bean("maintainPlanCodeGenerator")
    public BaseIdGenerator maintainPlanCodeGenerator() {
        return new DefaultIdGenerator(0, "WHP");
    }

    @Bean("maintainTaskCodeGenerator")
    public BaseIdGenerator maintainTaskCodeGenerator() {
        return new DefaultIdGenerator(0, "WHT");
    }

    @Bean
    public BaseIdGenerator inspectionRuleCodeGenerator() {
        return new DefaultIdGenerator(0, "IR");
    }

    @Bean
    public BaseIdGenerator classesItemCodeGenerator() {
        return new DefaultIdGenerator(0, "CL");
    }

    @Bean("stopServiceNoticeCodeGenerator")
    public BaseIdGenerator stopServiceNoticeCodeGenerator() {
        return new DefaultIdGenerator(0, "SSN");
    }

    @Bean
    public BaseIdGenerator incidentCodeGenerator() {
        return new DefaultIdGenerator(0, "INC");
    }

    @Bean
    public BaseIdGenerator institutionsCodeGenerator() {
        return new DefaultIdGenerator(0, "CO");
    }

    @Bean
    public BaseIdGenerator operationGroupCodeGenerator() {
        return new DefaultIdGenerator(0, "OG");
    }

    @Bean
    public BaseIdGenerator projectCodeGenerator() {
        return new DefaultIdGenerator(0, "PJ");
    }

    @Bean
    public BaseIdGenerator hnInspectionPlanCodeGenerator() {
        return new DefaultIdGenerator(0, "HNIP");
    }

    @Bean
    public BaseIdGenerator hnInspectionTaskCodeGenerator() {
        return new TimeIdGenerator(0, "MR");
    }

    @Bean
    public BaseIdGenerator transferWeeklyCodeGenerator() {
        return new DefaultIdGenerator(0, "TW");
    }

    @Bean("insuranceRegisterCodeGenerator")
    public BaseIdGenerator insuranceRegisterCodeGenerator() {
        return new DefaultIdGenerator(0, "ZBFW");
    }

    @Bean("insuranceRegisterTaskCodeGenerator")
    public BaseIdGenerator insuranceRegisterTaskCodeGenerator() {
        return new TimeIdGenerator(0, "ZBRW");
    }

    @Bean("insuranceInspectionTaskCodeGenerator")
    public BaseIdGenerator insuranceInspectionTaskCodeGenerator() {
        return new TimeIdGenerator(0, "ZB");
    }

    @Bean("insuranceReportTaskCodeGenerator")
    public BaseIdGenerator insuranceReportTaskCodeGenerator() {
        return new TimeIdGenerator(0, "ZBMR");
    }

    @Bean("insuranceSummaryTaskCodeGenerator")
    public BaseIdGenerator insuranceSummaryTaskCodeGenerator() {
        return new TimeIdGenerator(0, "ZBZJ");
    }

    @Bean
    public BaseIdGenerator hnInspectionHealthPlanCodeGenerator() {
        return new DefaultIdGenerator(0, "HNHXJ");
    }

    @Bean
    public BaseIdGenerator hnInspectionHealthTaskCodeGenerator() {
        return new TimeIdGenerator(0, "DQ");
    }

}
